package com.eastfair.builder.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.boot.request.PageParams;
import com.eastfair.builder.service.BuilderContractService;
import com.eastfair.core.base.R;
import com.eastfair.core.base.entity.SuperEntity;
import com.eastfair.pay.dto.ServiceContractPageQuery;
import com.eastfair.pay.dto.ServiceContractSaveDTO;
import com.eastfair.pay.dto.ServiceContractUpdateDTO;
import com.eastfair.pay.vo.ServiceContractVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * 合同
 * </p>
 *
 * @author lyw
 * @date 2022-07-29
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/contract")
@Api(value = "contract", tags = "合同")
public class ContractController {


    @Autowired
    private BuilderContractService builderContractService;



    /**
     * 保存合同
     * @param serviceContractSaveDTO
     * @return
     */
    @SysLog("添加合同")
    @PostMapping("/save")
    public R<Boolean> saveContract(@RequestBody @Validated ServiceContractSaveDTO serviceContractSaveDTO){
        return R.success(builderContractService.saveContract(serviceContractSaveDTO));
    }

    /**
     * 修改合同
     * @param serviceContractUpdateDTO
     * @return
     */
    @SysLog("修改合同")
    @PostMapping("/update")
    public R<Boolean> updateContract(@RequestBody @Validated({SuperEntity.Update.class}) ServiceContractUpdateDTO serviceContractUpdateDTO){

        return R.success(builderContractService.updateContract(serviceContractUpdateDTO));
    }


    /**
     * 合同付删除
     * @return
     */
    @SysLog("删除合同")
    @PostMapping("/delete")
    public R<Boolean> deleteContract(@RequestBody List<Long> ids){
        return R.success(builderContractService.deleteContract(ids));
    }

    /**
     * 合同列表
     * @param params
     * @return
     */
    @PostMapping("/queryPage")
    public R<Page<ServiceContractVO>> queryPage(@RequestBody @Validated PageParams<ServiceContractPageQuery> params){
        Page<ServiceContractVO> page = builderContractService.queryPage(params);
        return R.success(page);
    }

    /**
     * 通过合同id获取详情
     * @param id
     * @return
     */
    @GetMapping("/getContractById/{id}")
    public R<ServiceContractVO> getContractById(@PathVariable("id") Long id){
        ServiceContractVO serviceContractVO = builderContractService.getContractById(id);
        return R.success(serviceContractVO);
    }



}
